package cn.xenosp.server.common;

import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;



	/**
	 * 返回处理消息
	 */
	private String message = "";

	/**
	 * 返回代码
	 */
	private Integer code = Constant.OK;
	
	/**
	 * 返回数据对象 data
	 */
	private T data;
	
	/**
	 * 时间戳
	 */
	private long timestamp = System.currentTimeMillis();

	public Result() {
	}

    /**
     * 兼容VUE3版token失效不跳转登录页面
     * @param code
     * @param message
     */
	public Result(Integer code, String message) {
		this.code = code;
		this.message = message;
	}


	public static<T> Result<T> OK(String msg, T data) {
		Result<T> r = new Result<T>();
		r.setMessage(msg);
		r.setData(data);
		return r;
	}
	public static<T> Result<T> OK(String msg) {
		Result<T> r = new Result<T>();
		r.setMessage(msg);
		return r;
	}
	public static<T> Result<T> OK() {
		Result<T> r = new Result<T>();
		r.setMessage("成功");
		return r;
	}

	public static<T> Result<T> error(String msg, T data) {
		Result<T> r = new Result<T>();
		r.setCode(Constant.ERROR);
		r.setMessage(msg);
		r.setData(data);
		return r;
	}
	public static<T> Result<T> error(String msg) {
		Result<T> r = new Result<T>();
		r.setCode(Constant.ERROR);
		r.setMessage(msg);
		return r;
	}
	public static<T> Result<T> error() {
		Result<T> r = new Result<T>();
		r.setCode(Constant.ERROR);
		r.setMessage("失败");
		return r;
	}


}